# Packages
library(mdsr)
library(readr)
library(dplyr)
library(tidyr)
library(tidytext)
library(wordcloud)
library(readtext)
# Reading in Terrorism
terrorism <- read.table(unz("globalterrorismdb_0718dist.csv.zip", "globalterrorismdb_0718dist.csv"), nrows=181692, header=T, fill =T, quote="\"", sep=",")
# Reading in Econ Data
econData <- read.csv("WEO_Data.csv")
year_terrorism_deaths <-
terrorism %>%
group_by(country_txt) %>%
summarise(attacks = n(), deaths = sum(na.omit(nkill))) %>%
arrange(desc(deaths))
year_terrorism_deaths
# terrorism_words <-
# terrorism %>%
# select(attacktype1_txt, weaptype1_txt, weapsubtype1_txt, weapdetail)
#
# write.table(terrorism_words, file = "terrorism_words.txt", quote = FALSE,
# sep = " ", row.names = FALSE, col.names = FALSE)
#
# terrorism_txt <-
# readtext(file = "terrorism_words.txt") %>%
# as.tibble()
#
# head(terrorism_txt)
wordcloud("terrorism_words.txt", max.words = 10)
transformation drops documentstransformation drops documents

terrorism %>%
sample(5000) %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_point()

terrorism %>%
filter(country_txt == "United States") %>%
filter(longitude < 0) %>%
filter(nkillus > 0) %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_point(aes(color = nkill))

# Using apply to change values in data frame to numbers
econNumeric <- apply(X = econData[,5:30], FUN = as.numeric, MARGIN = 2)
NAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercionNAs introduced by coercion
econDataNew <-
econData %>%
select(c(1:4))
econData2 <-
as.data.frame(econNumeric)
econDataNew <-
econDataNew %>%
cbind(econData2)
econDataNew
# Using gather function to make data frame tidy
econDataTidy <-
econDataNew %>%
gather(key = "Year", value = "Value", 5:30)
econDataTidy
# Creating average of values over the 26 years found in the data frame
econAverages <-
econDataTidy %>%
group_by(Country, Subject.Descriptor, Units) %>%
summarise(average = mean(na.omit(Value)))
econAverages
# User-defined function to create data frame based off subject indicator
create_deaths_table <- function(x, y){
econAverages %>%
filter(Subject.Descriptor == x, Units == y) %>%
rename("country_txt" = "Country") %>%
left_join(year_terrorism_deaths, by = "country_txt") %>%
rename("Country" = "country_txt")
}
# Data frame with population and attacks/deaths
PopDeaths <- create_deaths_table("Population", "Persons")
Column `country_txt` joining factors with different levels, coercing to character vector
PopDeaths